package com.FH.applyForm.officeServies.advance;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/10/13.
 */
@Stateless
public class AdvanceDAOImpl extends AccessDB implements AdvanceDAO {
    /**
     * 查询预支费用
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getAdvanceInfo(String search, String page, String size) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from advance_name where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }
            if (json.has("advance_form_id")) {
                sql_w += " AND advance_form_id = '" + json.getString("advance_form_id") + "'";
            }
            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<AdvanceShowDTO> result = new ArrayList<AdvanceShowDTO>();
            if (!existName.equals("0")) {
                String sql = "select id,advancetypess_name,advance_form_id,advancetypess_price,advancetypess_id,advance_time,advance_desciption from advance_name  where 1=1" + sql_w +  sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    AdvanceShowDTO advanceShowDTO = new AdvanceShowDTO();
                    advanceShowDTO.setId(seresult.get(i)[1]);
                    advanceShowDTO.setAdvancetypess_name(seresult.get(i)[2]);
                    advanceShowDTO.setAdvance_form_id(seresult.get(i)[3]);
                    advanceShowDTO.setAdvancetypess_price(seresult.get(i)[4]);
                    advanceShowDTO.setAdvancetypess_id(seresult.get(i)[5]);
                    advanceShowDTO.setAdvance_time(seresult.get(i)[6]);
                    advanceShowDTO.setAdvance_desciption(seresult.get(i)[7]);

                    result.add(advanceShowDTO);
                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
            resultDTO.setStr(result.size() + "");
            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return resultDTO;

    }

    /**
     * 添加预支费用
     * @param advancePullDTO
     * @return
     */
    @Override
    public ResultDTO addAdvanceInfo(AdvancePullDTO advancePullDTO) {
        int resultls=0;
        ResultDTO resultDTO=new ResultDTO();

        String uuid= UUID.randomUUID().toString().trim().replaceAll("-", "");


        String Orgrole_insert="";
        Orgrole_insert="insert into advance_name (id,advancetypess_name,advance_form_id,advancetypess_price,advancetypess_id,advance_time,advance_desciption)" +
                "values('"+uuid+"','"+advancePullDTO.getAdvancetypess_name()+"','"+advancePullDTO.getAdvance_form_id()+"','"+advancePullDTO.getAdvancetypess_price()+"','"+advancePullDTO.getAdvancetypess_id()+"','"+advancePullDTO.getAdvance_time()+"','"+advancePullDTO.getAdvance_desciption()+"')";

        resultls=addReportingTo(Orgrole_insert);
        if(resultls!=0){
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        }else{
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }

    /**
     * 修改预支费用
     * @param advancePullDTO
     * @return
     */
    @Override
    public ResultDTO updateAdvanceInfo(AdvancePullDTO advancePullDTO) {

        ResultDTO resultDTO = new ResultDTO();
        String QUERY = "";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //更新sql语句
        QUERY += "update advance_name set ";
        if(null!=advancePullDTO.getAdvancetypess_name()&&!"".equals(advancePullDTO.getAdvancetypess_name())){
            QUERY += " advancetypess_name = '" + advancePullDTO.getAdvancetypess_name() + "'";
        }

        if(null!=advancePullDTO.getAdvance_form_id()&&!"".equals(advancePullDTO.getAdvance_form_id())){
            QUERY += ",advance_form_id ='" + advancePullDTO.getAdvance_form_id() + "'";
        }

        if(null!=advancePullDTO.getAdvancetypess_number()&&!"".equals(advancePullDTO.getAdvancetypess_number())){
            QUERY += ",advancetypess_number ='" + advancePullDTO.getAdvancetypess_number() + "'";
        }

        if(null!=advancePullDTO.getAdvancetypess_id()&&!"".equals(advancePullDTO.getAdvancetypess_id())){
            QUERY += ",advancetypess_id ='" + advancePullDTO.getAdvancetypess_id() + "'";
        }

        if(null!=advancePullDTO.getAdvancetypess_price()&&!"".equals(advancePullDTO.getAdvancetypess_price())){
            QUERY += ",advancetypess_price ='" + advancePullDTO.getAdvancetypess_price() + "'";
        }
        if(null!=advancePullDTO.getAdvance_time()&&!"".equals(advancePullDTO.getAdvance_time())){
            QUERY += ",advance_time ='" + advancePullDTO.getAdvance_time() + "'";
        }
        if(null!=advancePullDTO.getAdvance_desciption()&&!"".equals(advancePullDTO.getAdvance_desciption())){
            QUERY += ",advance_desciption ='" + advancePullDTO.getAdvance_desciption() + "'";
        }



        QUERY += " where advance_form_id = '" + advancePullDTO.getAdvance_form_id() + "'";


        int result = this.addReportingTo(QUERY);

        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;
    }

    /**
     * 删除预支费用
     * @param advancePullDTO
     * @return
     */
    @Override
    public ResultDTO deleteAdvanceInfo(AdvancePullDTO advancePullDTO) {
        String sql="select count(1) from advance_name WHERE advance_form_id='"+advancePullDTO.getAdvance_form_id()+"'";
        String existadvance= getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();
        // 删除SQL语句
        if(!existadvance.equals("0")){
            String QUERY = "";
            QUERY += "delete from advance_name  where advance_form_id='"+advancePullDTO.getAdvance_form_id()+"'";

            result = this.addReportingTo(QUERY);
        }

        // 判断SQl是否执行成功
        if (result!= 0) {

            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }
}
